import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-10-24
 * Time: 23:22
 */
public class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer,Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            if (!map.containsKey(nums[i])) {
                map.put(nums[i],i);
            }else {
                int j = map.get(nums[i]);
                if (Math.abs(i-j) <= k) {
                    return true;
                }
                map.put(nums[i],i);
            }
        }

        return false;
    }
}
